package com.zhuifeng.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.fastjson.JSON;
import com.zhuifeng.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhaoLi
 * @version 1.0.0
 * @ClassName ExcelUtils.java
 * @createTime 2022年08月06日 20:15:00
 */
public class ExcelUtils {
    /**
     * 读取Excel操作
     *
     * @return 包含Excel数据的list集合
     */

    public static <T> List<T> readFromExcelName(String stream, Class<T> clazz, int sheetNo) {

        //日志
        Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
        //解析的行数
        final Integer[] analysisCount = {0};
        ArrayList<T> list = new ArrayList<>();
        EasyExcel.read(stream)
                //反射类型
                .head(clazz)
                //读取的excel的sheet索引
                .sheet(sheetNo)
                //注册监听器
                .registerReadListener(new AnalysisEventListener<T>() {

                    @Override
                    public void invoke(T t, AnalysisContext analysisContext) {
                        logger.info("解析到一条数据:{}", JSON.toJSONString(t));
                        list.add(t);
                        analysisCount[0]++;
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                        logger.info("读取数据完毕,共解析数据:{}", analysisCount[0]);
                    }
                }).doRead();
        return list;
    }

    public static <T> Result<List<T>> readFromExcelBatch(String stream, Class<T> clazz) {
        //日志
        Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
        //解析的行数
        final Integer[] analysisCount = {0};
        //创建一个list集合
        ArrayList<T> list = new ArrayList<>();
        //读取文件
        ExcelReaderBuilder read = EasyExcel.read(stream);
        //获取反射的类型
        ExcelReaderBuilder head = read.head(clazz);
        //读取的excel的sheet索引
        List<ReadSheet> readSheets = head.build().excelExecutor().sheetList();
        //判断索引
        if (readSheets.size() <= 0) {
            return Result.error("excel.data.isEmpty");
        }
        //注册监听器
        head.registerReadListener(new AnalysisEventListener<T>() {

            @Override
            public void invoke(T t, AnalysisContext analysisContext) {
                logger.info("解析到一条数据:{}", JSON.toJSONString(t));
                list.add(t);
                analysisCount[0]++;
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                logger.info("读取数据完毕,共解析数据:{}", analysisCount[0]);
            }
        }).doReadAll();
        return Result.success(list);


    }

    /**
     * 将数据写入Excel
     *
     * @param clazz     使用的目标对象
     * @param fileName  下载时文件的名字
     * @param sheetName 写入到文件时对应的sheet的名称
     * @param dataList  要写入的数据的集合
     */
    public static void writeToExcel( String fileName,Class clazz,  String sheetName, List dataList) {

        EasyExcel.write(fileName,clazz).sheet(sheetName).doWrite(dataList);
    }
}
